#!/bin/sh
# qs, stands for Quick Script
# useful so that you can just do M-x qs, <number>, <ret>, and it'll
# run that command. This is the simple alternative to allowing for
# adding in quick commands in ait(1).
#
# Two notes:
#   1. this can only be ran in a shell by piping a value or entering
#      one in if ran. Ex: $ echo "" | qs
#   2. You can pass arguments for the program you want to run. For
#      example, if you want to run sort(1) but this time you want to
#      pass the -n flag you could do: M-x qs -n
#
# Generally, the way this is useful is that you keep qs as the default
# command for shell-command or open-command then you would just do a
# `esc x` <ret> number <ret> which is pretty quick compaired to typing
# it out but probably not as quick as a keybinding.
#
# requires pick(1) to be installed

value=""

# turns out read(1) doesn't like newlines and the -d flag isn't
# in bourne shell
if test -p /dev/stdin; then
  while IFS= read -r line; do
    if test "$value" = ""; then
      value="$line"
    else
      value="$value\n$line"
    fi
  done
fi

output=`cat <<EOF
1. align
2. ff
3. fmt -w 70
4. gff
5. gg
6. ggt
7. spell
8. sort
9. xclip -selection clipboard
EOF`

cmd=`echo "$output" | pick | cut -d' ' -f2-`

echo "$value" | $cmd $@
